package org.xxpay.pay.channel.sqpay.util;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtil{
    
    private static CloseableHttpClient httpClient = null;
    public static final String CHARSET = "UTF-8";
	
    static HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
            return true;
        }
    };
    
    public String[] doPostQueryCmd(String strURL, Map<String, String> req) {
        
        String returncode = null;
        String result = null;
        String[] resultarr = new String[2];
        HttpPost httpPost = null;
        
        try{
            List<NameValuePair> pairs = null;
            if(req != null && !req.isEmpty()){
                pairs = new ArrayList<NameValuePair>(req.size());
                for(Map.Entry<String,String> entry : req.entrySet()){
                    String value = entry.getValue();
                    if(value != null){
                        pairs.add(new BasicNameValuePair(entry.getKey(),value));
                    }
                }
            }
            
            httpPost = new HttpPost(strURL);
            
            if(pairs != null && pairs.size() > 0){
                httpPost.setEntity(new UrlEncodedFormEntity(pairs,CHARSET));
            }
            
            httpPost.setHeader("Accept-Encoding", "gzip, deflate");
            httpPost.setHeader("Accept-Language", "zh-CN");
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            httpPost.setHeader("Accept", "application/json, text/html, text/*, */*");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
            
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            
            CloseableHttpResponse response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            
            if (statusCode != 200) {
                    httpPost.abort();
                    throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            try{
                HttpEntity entity = response.getEntity();
                returncode = Integer.toString(statusCode);
                try {
                    if (entity != null){
                        result = EntityUtils.toString(entity, "utf-8");
                    }
                }finally {
                      if(entity != null){
                          entity.getContent().close();
                      }
                }
            } finally {
                if(response != null){
                    response.close();
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally{
            httpPost.releaseConnection();
        }
        
        if (result == null) {
            result = "";
        }
        
        resultarr[0] = returncode;
        resultarr[1] = result;
        
        return resultarr;
    }
    
}
